package com.shilei.proxy.dynamicProxy.cglib;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;


/**
 * @description:  cglib（code generate library） 实现
 *                 实现动态代理不需要接口
 *
 *                 坏处  不能对Final修饰的类进行代理  因为cglib实现动态代理用的是继承代理的类  final class 不能继承
 * @author: 81271
 * @date: 2020-04-18 18:38
 */
public class Tank {

    public void move() {
        System.out.println("Tank running kakakakakakakaka");
    }
}



class Main {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Tank.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("before");
                Object o1 = methodProxy.invokeSuper(o, objects);
                System.out.println("after");
                return  o1;
            }
        });

        Tank tank = (Tank)enhancer.create();
        tank.move();
    }
}
